﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WinForms;
using MarginTrading.Common;

namespace MarginTrading.Forms
{
    public partial class SellSettlementSummary : Form
    {
        private class Item
        {
            public string Name;
            public int Value;
            public Item(string name, int value)
            {
                Name = name; Value = value;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }

        public SellSettlementSummary()
        {
            InitializeComponent();
        }

        public SellSettlementSummary(string agent, DateTime date)
        {
            InitializeComponent();

            button1.Enabled = false;
            LoadReport(agent, date);
            button1.Enabled = true;
            ddlAgent.SelectedText = agent;
            dateTimePicker1.Value = date;
        }

        private void DebtorsFollowup_Load(object sender, EventArgs e)
        {
            DataTable agent = AgentServices.GetAgentCollectionDataTable();
            DataRow dr = agent.NewRow();
            dr["CODE"] = "All";
            agent.Rows.InsertAt(dr, 0);
            ddlAgent.DisplayMember = "CODE";
            ddlAgent.ValueMember = "CODE";
            ddlAgent.DataSource = agent;
           // ddlAgent.Items.Add(new Item("dd",0));
        }

        private void LoadReport(string agentCode, DateTime date)
        {
            try
            {
                Agent agent = AgentServices.GetAgentByCode(agentCode);

                if (agent == null)
                {
                    agent = new Agent();
                }
                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                if (agentCode != "All")
                {
                    ds.Value = ReportServices.GetSSSummaryByAgentAndDate(agentCode, date);
                }
                else
                {
                    ds.Value = ReportServices.GetSSSummaryByAgentAndDate(date);
                    agent.Name = "All";
                }

                this.reportViewer1.LocalReport.ReportPath = "SellSettlementSummary.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("Date1",date == DateTime.MinValue ? "" : date.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("AgentName",agent.Name ),
                        new ReportParameter("ReportTitle",string.Format(Reports.SETTLEMENT_SUMMARY,date.ToString(DateFormat.DATE_FORMAT)))});

                this.reportViewer1.RefreshReport();
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;
            }
            catch (System.Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            LoadReport(ddlAgent.SelectedValue.ToString(), dateTimePicker1.Value);
            button1.Enabled = true;
        }
    }
}
